import 'dart:io';

import 'package:casdon/app/config/theme/theme_extention.dart';
import 'package:casdon/build_gen/assets.gen.dart';
import 'package:casdon/common/widget/safe_cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

typedef MediaTapCallback = void Function(String url, bool isVideo);

class FeedbackMediaPreview extends StatelessWidget {
  final String source;
  final bool isVideo;
  final VoidCallback? onDelete;
  final MediaTapCallback? onTap;
  final Widget Function(String path)? customVideoBuilder;
  final double? width;
  final double? height;

  const FeedbackMediaPreview({
    super.key,
    required this.source,
    required this.isVideo,
    this.onDelete,
    this.onTap,
    this.customVideoBuilder,
    this.width,
    this.height,
  });

  bool _isNetwork(String path) {
    final uri = Uri.tryParse(path);
    return uri != null && uri.hasScheme && uri.host.isNotEmpty;
  }

  @override
  Widget build(BuildContext context) {
    final isNetworkSource = _isNetwork(source);
    final double effectiveWidth = width ?? 80.w;
    final double effectiveHeight = height ?? effectiveWidth;
    final borderRadius = BorderRadius.circular(8.r);

    Widget buildImage() {
      if (isNetworkSource) {
        return SafeCachedNetworkImage(imageUrl: source, fit: BoxFit.cover);
      }
      return Image.file(
        File(source),
        fit: BoxFit.cover,
        errorBuilder: (_, __, ___) => const Icon(Icons.image, size: 40),
      );
    }

    return SizedBox(
      width: effectiveWidth,
      height: effectiveHeight,
      child: GestureDetector(
        onTap: () => onTap?.call(source, isVideo),
        child: Stack(
          fit: StackFit.expand,
          children: [
            Container(
              decoration: BoxDecoration(
                borderRadius: borderRadius,
                border: Border.all(
                  color:
                      context.customColorThemeNonNull.gray2 ??
                      Colors.grey.shade300,
                ),
              ),
              child: ClipRRect(
                borderRadius: borderRadius,
                child: isVideo
                    ? customVideoBuilder?.call(source) ??
                          _DefaultVideoThumbnail(videoPath: source)
                    : buildImage(),
              ),
            ),
            if (isVideo)
              Positioned.fill(
                child: IgnorePointer(
                  child: ClipRRect(
                    borderRadius: borderRadius,
                    child: Container(
                      color: Colors.black.withOpacity(0.25),
                      child: const Center(
                        child: Icon(
                          Icons.play_circle_filled,
                          color: Colors.white,
                          size: 30,
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            if (onDelete != null)
              Positioned(
                top: 0.w,
                right: 0.w,
                child: GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () => onDelete?.call(),
                  child: Assets.images.picDel.image(
                    width: 20.w,
                    height: 20.w,
                    fit: BoxFit.contain,
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }
}

class _DefaultVideoThumbnail extends StatefulWidget {
  final String videoPath;
  const _DefaultVideoThumbnail({required this.videoPath});

  @override
  State<_DefaultVideoThumbnail> createState() => _DefaultVideoThumbnailState();
}

class _DefaultVideoThumbnailState extends State<_DefaultVideoThumbnail> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.black.withOpacity(0.15),
      alignment: Alignment.center,
      child: const Icon(Icons.videocam, color: Colors.white70),
    );
  }
}
